package vip.xiaonuo.modular.model;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.client.j2se.MatrixToImageWriter;

public class GenerateBarcode {
    public static void main(String[] args) throws Exception {
        String barcodeText = "123456789"; // The text to encode
        String outputFile = "D:/IdeaProjects/snowy-layui-origin/image.png"; // The file to save the image to
        int width = 300; // The width of the barcode
        int height = 100; // The height of the barcode

        // Set the barcode format and encoding parameters
        BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;
        EncodeHintType hintType = EncodeHintType.ERROR_CORRECTION;
        int hintValue = 0; // Disable error correction

        // Create the barcode writer and encode the text
        Code128Writer barcodeWriter = new Code128Writer();
        BitMatrix matrix = barcodeWriter.encode(barcodeText, barcodeFormat, width, height);

        // Create the barcode image and save it to a file
        BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix);
        File output = new File(outputFile);
        ImageIO.write(image, "png", output);
    }
}
